<?php
namespace Admin\Controller;
use Admin\Controller\BaseController;
use Think\Model;

class ChargerController extends BaseController {
    //put your code here
    function chglist(){
    	$CHG = new Model('Charger');
        $totalRows = $CHG->count();
    	$page = new \Think\Page($totalRows, 10);
    	$chglist = $CHG->limit($page->firstRow,$page->listRows)->order('id desc')->select();
        $show = $page->show();
        $this->assign('page',$show);
        $this->assign('chglist',$chglist);
    	$this->display('charger-chglist');
    }

    function insert(){
    	if(IS_POST){
            $CHG = D('Charger');
            if(!$CHG->create()){
                $this->error($CHG->getError());
            } else {
                $rs = $CHG->add();
                if($rs !== FALSE){
                    $this->success('增加成功');
                } else {
                    $this->error('增加失败');
                }
            }
    	}else{
    		$this->display('charger-insert');
    	}
    }

    function edit(){
    	if(IS_POST){
			$id = I('id','');
            if($id != ''){
                $map['id'] = $id;
                if(I('name') != ''){
                    $map['name'] = I('name');
                }
                if(I('email') != ''){
                    $map['email'] = I('email');
                }
                if(I('mobile') != ''){
                    $map['mobile'] = I('mobile');
                }
                $map['status'] = I('status');
                $CHG = D('Charger');
                if(!$CHG->create($map)){
                    $this->error($CHG->getError());
                } else {
                    $rs = $CHG->save();
                    if($rs !== FALSE){
                        $this->success('编辑成功');
                    } else {
                        $this->error('编辑失败');
                    }
                }
            } else {
                $this->error('参数错误');
            }
    	}else{
    		$id = I('id','');
            if($id != ''){
                $CHG = new Model('Charger');
                $edit = $CHG->where(['id'=>$id])->find();
                if($edit){
                    $this->assign('edit',$edit);
                    $this->display('charger-edit');
                } else {
                    $this->error('维护员员不存在');
                }
            }else{
                $this->error('参数错误');
            }
    	}
    }

    function delete(){
		$id = I('id','');
        if($id == ''){
            $this->error('参数错误');
        } else {
            #2，删除操作
            $CHG = new Model('Charger');
            $rs = $CHG->where(['id'=>$id])->delete();
            if($rs !== FALSE){
                $this->success('删除成功');
            } else {
                $this->error($CHG->getDbError());
            }
        }
    }

    function status(){
		$id = I('id','');
        $op = I('op') ? 1 : 0;
        if($id != ''){
            $CHG = new Model('Charger');
            $rs = $CHG->where(['id'=>$id])->setField('status',$op);
            if($rs !== false){
                $this->success('操作成功');
            }else{
                $this->error('操作失败，稍后再试');
            }
        }else{
            $this->error('参数错误');
        }
    }

}
